/**
 * Created by Jacky.Gao on 2017-01-25.
 */
import Tool from './Tool.js';
import {
    tableToXml,
    getUrlParam
} from '../Utils.js';
import {
    alert
} from '../MsgBox.js';

export default class PreviewTool extends Tool {
    execute() { }
    buildButton() {
        const group = $(`<div class="btn-group"></div>`);
        const mainBtn = $(`<button type="button" class="btn btn-default dropdown-toggle" style="border:none;border-radius:0;padding: 0px 10px; height:40px;line-height:40px" data-toggle="dropdown" title="${window.i18n.tools.preview.preview}">
             <i class="icon-ym icon-ym-report-icon-preview"></i>
             <i class="icon-ym icon-ym-report-icon-arrow-down" style="font-size: 12px;"></i>
         </button>`);
        const ul = $(`<ul class="dropdown-menu" role="menu"></ul>`);
        const preview = $(`<li>
                 <a href="javascript:;">
                     <i class="icon-ym icon-ym-report-icon-preview"></i> ${window.i18n.tools.preview.view}
                 </a>
             </li>`);
        ul.append(preview);
        const _this = this;
        preview.click(function () {
            _this.doPreview();
        });
        const pagingPreview = $(`<li>
                 <a href="javascript:;">
                     <i class="icon-ym icon-ym-report-icon-page-preview"></i> ${window.i18n.tools.preview.pagingPreview}
                 </a>
             </li>`);
        ul.append(pagingPreview);
        pagingPreview.on('click', () => {
            _this.doPreview(true);
        });
        group.append(mainBtn);
        group.append(ul);
        return group;
    }
    listQuery(list, callback) {
        for (let i = 0; i < list.length; i++) {
            let item = list[i]
            console.log(item)
            let arrayList = []
            if (item.hasOwnProperty('cols') && Array.isArray(item.cols)) {
                arrayList = arrayList.concat(item.cols)
            }
            if (item.hasOwnProperty('children') && Array.isArray(item.children)) {
                arrayList = arrayList.concat(item.children)
            }
            if (item.bindParameter && item.defaultValue) {
                callback[item.bindParameter] = item.defaultValue
            }
            this.listQuery(arrayList, callback)
        }
    }
    doPreview(withPaging) {
        const token = getUrlParam('token')
        let targetUrl = `${window._contextPath}/preview.html?id=preview&from=menu&token=${token}`;
        if (withPaging) {
            targetUrl += '&page=1';
        }
        const content = tableToXml(this.context);
        const item = {};
        if(this.context.reportDef.searchForm) {
            this.listQuery(this.context.reportDef.searchForm.components, item);
        }
        for (let key in item) {
            let item1 = '&' + key + '=' + item[key]
            targetUrl += item1
        }
        $.ajax({
            url: window._server + "/designer/savePreviewData",
            type: 'POST',
            data: {
                content
            },
            headers: {
                'Authorization': token
            },
            success: function (data) {
                // 增加后端异常处理
                if (data && data.code !== 200) {
                    alert(`${data.msg}`);
                } else {
                    let newWindow = window.open(targetUrl, "_blank");
                    newWindow && newWindow.focus();
                }
            },
            error: function () {
                alert(`${window.i18n.tools.preview.previewFail}`);
            }
        });
    }
    getTitle() {
        return `${window.i18n.tools.preview.preview}`;
    }
    getIcon() {
        return `<i class="ureport ureport-preview" style="color: #0e90d2;"></i>`;
    }
}
